package com.dw.article.service.impl;

import cn.hutool.core.util.IdUtil;
import com.alibaba.nacos.shaded.com.google.common.collect.Lists;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dw.article.mapper.mp.LabelInfoMapper;
import com.dw.model.article.dtos.label.AddLabelDTO;
import com.dw.model.article.dtos.label.AddLabelHotDTO;
import com.dw.model.article.dtos.label.QueryLabelDTO;
import com.dw.model.article.entitys.LabelInfo;
import com.dw.article.service.LabelInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 文章标签表(LabelInfo)表服务实现类
 *
 * @author makejava
 * @since 2024-10-08 15:20:03
 */
@Service("labelInfoService")
public class LabelInfoServiceImpl extends ServiceImpl<LabelInfoMapper, LabelInfo> implements LabelInfoService {

    @Resource
    private RedisTemplate redisTemplate;

    private final String LABEL_HOT = "label.hot";

    /**
     * 新增标签
     */
    @Override
    public Boolean addLabel(AddLabelDTO addLabelDTO) {
        // 参数拷贝
        LabelInfo labelInfo = new LabelInfo();
        BeanUtils.copyProperties(addLabelDTO, labelInfo);
        // 设置id
        long l = IdUtil.getSnowflake().nextId();
        labelInfo.setLeiId(l);
        return save(labelInfo);
    }

    @Override
    public List<LabelInfo> queryLabel(QueryLabelDTO queryLabelDTO) {
        // 条件查询
        List<LabelInfo> labelInfos = lambdaQuery()
                .like(LabelInfo::getLabelName, queryLabelDTO.getLabelName())
                .list();
        // 分页查询
        List<List<LabelInfo>> partition = Lists.partition(labelInfos, queryLabelDTO.getPageSize());
        return partition.get(queryLabelDTO.getCurrent() - 1);
    }

    @Override
    public List<LabelInfo> queryLabelByIds(List<Long> labelIds) {
        return labelIds.stream().map(item -> {
            LabelInfo one = lambdaQuery()
                    .eq(LabelInfo::getLeiId, item)
                    .one();
            return one;
        }).collect(Collectors.toList());
    }

    /**
     * 根据标签id查询标签名称
     */
    @Override
    public LabelInfo queryLabelNameById(Long labelId) {
        return lambdaQuery()
                .eq(LabelInfo::getLeiId, labelId)
                .one();
    }

}

